<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2017/7/24
 * Time: 21:27
 */

namespace Api\Service;


use Think\Page;

class SheetService
{
    /**
     * 分页获取谱单内曲谱详情
     * @param $scoreId
     * @return mixed
     */
    public function getSheetListByScoreId($scoreId, $param)
    {
        //获取曲谱详情
        $map = array(
            'score_id' => $scoreId,
            'is_delete' => 0
        );
        $res = M('ScoreSheet')->where($map)->select();
        $sheetIds = array_column($res, 'sheet_id');
        $map = array(
            'id' => array('in', $sheetIds),
            'status' => array('neq', -1),
            'is_delete' => 0
        );
        $sheetModel = M('Sheet');
        $count = $sheetModel->where($map)->count();
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $page = new PageService($param, $count, $listRows);
        $show = $page->show();

        $sheetList = $sheetModel->where($map)
            ->field('id,name,singer,song_name')
            ->order('id')->limit($page->firstRow.','.$page->listRows)
            ->select();
        $list = array();
        foreach ($sheetList as $key => $val) {
            $list['list'][$key] = $val;
        }
        $list['nowPage'] = $page->nowPage;
        $list['totalCount'] = $count;
        $list['totalPage'] = ceil($count / $listRows);

        return $list;
    }

    /**
     * 限定条件分页获取曲谱
     * @param $map 查询条件
     * @param int $listRows 每页显示行数
     * @return mixed
     */
    public function getSheetListByMap($map, $param)
    {
        $model = M('Sheet');
        $count = $model->where($map)->count();

        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $page = new PageService($param, $count, $listRows);
        $show = $page->show();

        $sheetList = $model->where($map)
            ->field('id, name, singer, song_name')
            ->order('id')->limit($page->firstRow.','.$page->listRows)
            ->select();
        foreach ($sheetList as $key => $val) {
            $list['list'][$key] = $val;
        }
        if (empty($list['list'])) {
            $list['list'] = array();
        }
        $list['nowPage'] = $page->nowPage;
        $list['totalCount'] = $count;
        $list['totalPage'] = ceil($count / $listRows);
        return $list;
    }

    /**
     * 获取曲谱基本信息
     * @param $sheetIds
     * @return array
     */
    public function getMapBySheetIds($sheetIds)
    {
        $map = array(
            'id' => array('in', $sheetIds),
            'is_delete' => 0
        );
        $creatorList = M('Sheet')->where($map)->field('id, name, singer, song_name')->select();

        $sheetMap = array();
        foreach ($creatorList as $key => $val) {
            $sheetMap[$val['id']] = $val;
        }
        return $sheetMap;
    }



}